package com.bmob.lover.ui.adapter

import android.app.Activity
import android.text.TextUtils
import android.view.View
import androidx.recyclerview.widget.DiffUtil
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.hjq.toast.ToastUtils
import com.qmuiteam.qmui.widget.QMUIRadiusImageView2
import com.qmuiteam.qmui.widget.dialog.QMUIBottomSheet
import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer
import com.shuyu.gsyvideoplayer.video.base.GSYVideoView
import com.theone.common.ext.*
import com.theone.common.util.DateFormatUtils
import com.theone.common.widget.TheNineGridLayout
import com.bmob.lover.R
import com.bmob.lover.app.widget.LoverVideoPlayer
import com.bmob.lover.data.model.bean.Lover
import com.luck.picture.lib.config.SelectMimeType
import com.theone.mvvm.core.app.ext.qmui.OnGridBottomSheetItemClickListener
import com.theone.mvvm.core.app.ext.qmui.showGridBottomSheet
import com.theone.mvvm.core.app.util.DownloadUtil.startDownload
import com.theone.mvvm.core.data.entity.QMUIItemBean
import com.theone.mvvm.core.ui.binding_adapter.CoreBindAdapter.imageUrl

//  ┏┓　　　┏┓
//┏┛┻━━━┛┻┓
//┃　　　　　　　┃
//┃　　　━　　　┃
//┃　┳┛　┗┳　┃
//┃　　　　　　　┃
//┃　　　┻　　　┃
//┃　　　　　　　┃
//┗━┓　　　┏━┛
//    ┃　　　┃                  神兽保佑
//    ┃　　　┃                  永无BUG！
//    ┃　　　┗━━━┓
//    ┃　　　　　　　┣┓
//    ┃　　　　　　　┏┛
//    ┗┓┓┏━┳┓┏┛
//      ┃┫┫　┃┫┫
//      ┗┻┛　┗┻┛
/**
 * @author The one
 * @date 2021-04-22 14:06
 * @describe TODO
 * @email 625805189@qq.com
 * @remark
 */
class Lover2Adapter(val showTopBar: Boolean) : BaseMultiItemQuickAdapter<Lover, BaseViewHolder>(), LoadMoreModule {

    companion object {
        val TAG_SAVE = "下载"
        val TAG_LINK = "复制链接"
    }

    private var curPlayer: StandardGSYVideoPlayer? = null
    private var isPlay = false

    init {
        addItemType(SelectMimeType.TYPE_ALL, R.layout.item_lover_text)
        addItemType(SelectMimeType.TYPE_IMAGE, R.layout.item_lover_image)
        addItemType(SelectMimeType.TYPE_VIDEO, R.layout.item_lover_video)
        setDiffCallback(object : DiffUtil.ItemCallback<Lover>() {

            override fun areItemsTheSame(oldItem: Lover, newItem: Lover): Boolean {
                return oldItem.objectId == newItem.objectId
            }

            override fun areContentsTheSame(oldItem: Lover, newItem: Lover): Boolean {
                return oldItem == newItem
            }

        })
    }

    private val mActions = listOf<QMUIItemBean>(
        QMUIItemBean(TAG_SAVE, R.drawable.svg_operation_save),
        QMUIItemBean(TAG_LINK, R.drawable.svg_lover_link)
    )

    override fun convert(holder: BaseViewHolder, item: Lover) {
        with(holder) {
            if (itemViewType != SelectMimeType.TYPE_VIDEO) {
                val icon = getView<QMUIRadiusImageView2>(R.id.iv_icon)
                setText(R.id.tv_location, item.getUserAddress())
                setText(R.id.tv_nickname, "@${item.author?.nickname}")
                setText(R.id.tv_content, item.content)
                setGone(R.id.tv_content, TextUtils.isEmpty(item.content))
                imageUrl(icon, item.author?.avatar)
                item.createdAt?.formatLong(
                    YYYY_MM_DD_HH_MM_SS
                )?.let {
                    setText(
                        R.id.tv_time,
                        DateFormatUtils.formatTimeStampString(
                            context,
                            it,
                            DateFormatUtils.FORMAT_TYPE_PERSONAL_FOOTPRINT
                        )
                    )
                }
                if (itemViewType == SelectMimeType.TYPE_IMAGE) {
                    getView<TheNineGridLayout>(R.id.nine_grid).setUrlList(item.getFiles())
                }
            } else {
                initVideoPlayer(
                    getView<LoverVideoPlayer>(R.id.video_player),
                    item,
                    holder.absoluteAdapterPosition
                )
            }
        }

    }

    private fun initVideoPlayer(
        coverVideo: LoverVideoPlayer,
        item: Lover,
        position: Int
    ) {
        with(coverVideo) {
            setVideoData(item)
            setOnIconClickListener {
                showVideoActionDialog(item)
            }
            playTag = item.getFiles()[0].getImageUrl()+showTopBar.toString()
            playPosition = position
            setVideoAllCallBack(object : GSYSampleCallBack() {

                override fun onPrepared(url: String, vararg objects: Any) {
                    super.onPrepared(url, *objects)
                    curPlayer = objects[1] as LoverVideoPlayer
                    isPlay = true
                }

                override fun onAutoComplete(url: String, vararg objects: Any) {
                    super.onAutoComplete(url, *objects)
                    curPlayer = null
                    isPlay = false
                }

            })
        }

    }

    private fun showVideoActionDialog(item: Lover) {
        context.showGridBottomSheet(
            mActions,
            listener = object : OnGridBottomSheetItemClickListener<QMUIItemBean> {
                override fun onGridBottomSheetItemClick(
                    dialog: QMUIBottomSheet,
                    itemView: View,
                    tag: QMUIItemBean
                ) {
                    dialog.dismiss()
                    when (tag.title) {
                        TAG_SAVE -> startDownload(
                            context as Activity,
                            item.getFiles()[0]
                        )
                        TAG_LINK -> {
                            item.getFiles()[0].path.setPrimaryClip(context)
                            ToastUtils.show("链接已复制到剪切板")
                        }
                        else -> {
                        }
                    }
                }
            }).show()
    }

    fun onResume() {
        curPlayer?.currentPlayer?.let {
            if(it.currentState == GSYVideoView.CURRENT_STATE_PAUSE){
                it.onVideoResume()
            }else{
                it.startPlayLogic()
            }
        }
    }

    fun onPause() {
        if (isPlay) {
            curPlayer?.currentPlayer?.onVideoPause()
        }
    }

    fun onDestroy() {
        if (isPlay) {
            curPlayer?.currentPlayer?.release()
        }
    }

}